home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gnuchess.lha / Xchess / window.c.bm < prev    next >
Text File  |  1990-05-13  |  25KB  |  929 lines

  1. /* This file contains code for X-CHESS.
  2.    Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4. This file is part of X-CHESS.
  5.  
  6. X-CHESS is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the X-CHESS General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. X-CHESS, but only under the conditions described in the
  15. X-CHESS General Public License.   A copy of this license is
  16. supposed to have been given to you along with X-CHESS so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. /* RCS Info: $Revision: 1.5 $ on $Date: 86/11/26 12:11:15 $
  23.  *           $Source: /users/faustus/xchess/RCS/window.c,v $
  24.  * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
  25.  *    Permission is granted to do anything with this code except sell it
  26.  *    or remove this message.
  27.  *
  28.  * Deal with the two (or one) windows.
  29.  */
  30.  
  31. #include "xchess.h"
  32. #include <sys/time.h>
  33.  
  34. #include "pawn.bitmap"
  35. #include "rook.bitmap"
  36. #include "knight.bitmap"
  37. #include "bishop.bitmap"
  38. #include "queen.bitmap"
  39. #include "king.bitmap"
  40.  
  41. #include "pawn_outline.bitmap"
  42. #include "rook_outline.bitmap"
  43. #include "knight_outline.bitmap"
  44. #include "bishop_outline.bitmap"
  45. #include "queen_outline.bitmap"
  46. #include "king_outline.bitmap"
  47.  
  48. #include "pawn_mask.bitmap"
  49. #include "rook_mask.bitmap"
  50. #include "knight_mask.bitmap"
  51. #include "bishop_mask.bitmap"
  52. #include "queen_mask.bitmap"
  53. #include "king_mask.bitmap"
  54.  
  55. #include "shade.bitmap"
  56.  
  57. #include "xchess.cur"
  58. #include "xchess_mask.cur"
  59.  
  60. #include "xchess.icon"
  61.  
  62. windata *win1, *win2;
  63. bool win_flashmove = false;
  64.  
  65. extern bool setup();
  66. extern void service(), drawgrid(), icon_refresh();
  67.  
  68. bool
  69. win_setup(disp1, disp2)
  70.     char *disp1, *disp2;
  71. {
  72.     win1 = alloc(windata);
  73.     if (!oneboard)
  74.         win2 = alloc(windata);
  75.  
  76.     if (!setup(disp1, win1) || (!oneboard && !setup(disp2, win2)))
  77.         return (false);
  78.  
  79.     if (blackflag) {
  80.         win1->color = BLACK;
  81.         win1->flipped = true;
  82.     } else
  83.         win1->color = WHITE;
  84.     win_drawboard(win1);
  85.  
  86.     if (!oneboard) {
  87.         win2->color = BLACK;
  88.         win2->flipped = true;
  89.         win_drawboard(win2);
  90.     }
  91.     
  92.     return(true);
  93. }
  94.  
  95. /* Draw the chess board... */
  96.  
  97. void
  98. win_drawboard(win)
  99.     windata *win;
  100. {
  101.     int i, j;
  102.  
  103.     drawgrid(win);
  104.  
  105.     /* Now toss on the squares... */
  106.     for (i = 0; i < SIZE; i++)
  107.         for (j = 0; j < SIZE; j++)
  108.             win_erasepiece(j, i, win->color);
  109.  
  110.     return;
  111. }
  112.  
  113. /* Draw one piece. */
  114.  
  115. void
  116. win_drawpiece(p, y, x, wnum)
  117.     piece *p;
  118.     int y, x;
  119.     color wnum;
  120. {
  121.     char *bits, *maskbits, *outline;
  122.     windata *win;
  123.     char buf[BSIZE];
  124.     XImage *tmpImage;
  125.     Pixmap tmpPM, maskPM;
  126.     XGCValues gc;
  127.  
  128.     if (oneboard || (wnum == win1->color))
  129.     win = win1;
  130.     else
  131.     win = win2;
  132.  
  133.     if (win->flipped) {
  134.     y = SIZE - y - 1;
  135.     x = SIZE - x - 1;
  136.     }
  137.  
  138.     /*
  139.       if (debug)
  140.       fprintf(stderr, "draw a %s at (%d, %d) on board %d\n",
  141.       piecenames[(int) p->type], y, x, wnum);
  142.       */
  143.  
  144.     if ((x < 0) || (x > 7) || (y < 0) || (y > 7)) exit(1);
  145.  
  146.     switch (p->type) {
  147.     case PAWN:
  148.     bits = pawn_bits;
  149.     maskbits = pawn_mask_bits;
  150.     outline = pawn_outline_bits;
  151.     break;
  152.  
  153.     case ROOK:
  154.     bits = rook_bits;
  155.     maskbits = rook_mask_bits;
  156.     outline = rook_outline_bits;
  157.     break;
  158.  
  159.     case KNIGHT:
  160.     bits = knight_bits;
  161.     maskbits = knight_mask_bits;
  162.     outline = knight_outline_bits;
  163.     break;
  164.  
  165.     case BISHOP:
  166.     bits = bishop_bits;
  167.     maskbits = bishop_mask_bits;
  168.     outline = bishop_outline_bits;
  169.     break;
  170.  
  171.     case QUEEN:
  172.     bits = queen_bits;
  173.     maskbits = queen_mask_bits;
  174.     outline = queen_outline_bits;
  175.     break;
  176.  
  177.     case KING:
  178.     bits = king_bits;
  179.     maskbits = king_mask_bits;
  180.     outline = king_outline_bits;
  181.     break;
  182.  
  183.     default:
  184.     fprintf(stderr,
  185.         "Internal Error: win_drawpiece: bad piece type %d\n",
  186.         p->type);
  187.     }
  188.  
  189.     /* There are two things we can do... If this is a black and white
  190.      * display, we have to shade the square and use an outline if the piece
  191.      * is white.  We also have to use a mask...  Since we don't want
  192.      * to use up too many bitmaps, create the mask bitmap, put the bits,
  193.      * and then destroy it.
  194.      */
  195.     if (win->bnw && (p->color == WHITE))
  196.     bits = outline;
  197.     if (win->bnw && !iswhite(win, x, y)) {
  198.     XSetState(win->display, DefaultGC(win->display, 0),
  199.           BlackPixel(win->display, 0),
  200.           WhitePixel(win->display, 0), GXcopy, AllPlanes);
  201.     
  202.     tmpPM = XCreateBitmapFromData(win->display, win->boardwin,
  203.                 shade_bits, SQUARE_WIDTH, SQUARE_HEIGHT);
  204.  
  205.     XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),
  206.            0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,
  207.            x * (SQUARE_WIDTH + BORDER_WIDTH),
  208.            y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);
  209.  
  210.     XFreePixmap(win->display, tmpPM);
  211.     
  212.     XSetFunction(win->display, DefaultGC(win->display, 0),
  213.              GXandInverted);
  214.     maskPM = XCreateBitmapFromData(win->display, win->boardwin,
  215.                       maskbits, SQUARE_WIDTH, SQUARE_HEIGHT);
  216.     XCopyPlane(win->display, maskPM, win->boardwin, DefaultGC(win->display, 0),
  217.            0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,
  218.            x * (SQUARE_WIDTH + BORDER_WIDTH),
  219.            y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);
  220.     XFreePixmap(win->display, maskPM);
  221.  
  222.     XSetFunction(win->display, DefaultGC(win->display, 0),
  223.              GXor);
  224.     tmpPM = XCreateBitmapFromData(win->display, win->boardwin,
  225.                 bits, SQUARE_WIDTH, SQUARE_HEIGHT);
  226.     XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),
  227.            0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,
  228.            x * (SQUARE_WIDTH + BORDER_WIDTH),
  229.            y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);
  230.     XFreePixmap(win->display, tmpPM);
  231.  
  232.     XSetFunction(win->display, DefaultGC(win->display, 0), GXcopy);
  233.  
  234.     } else if (win->bnw){
  235.     XSetState(win->display, DefaultGC(win->display, 0),
  236.           BlackPixel(win->display, 0),
  237.           WhitePixel(win->display, 0), GXcopy, AllPlanes);
  238.  
  239.     tmpPM = XCreateBitmapFromData(win->display, win->boardwin,
  240.                 bits, SQUARE_WIDTH, SQUARE_HEIGHT);
  241.     XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),
  242.            0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,
  243.            x * (SQUARE_WIDTH + BORDER_WIDTH),
  244.            y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);
  245.     XFreePixmap(win->display, tmpPM);
  246.     } else {
  247.     XSetState(win->display, DefaultGC(win->display, 0),
  248.          ((p->color == WHITE) ? win->whitepiece.pixel :
  249.                       win->blackpiece.pixel),
  250.           (iswhite(win, x, y) ? win->whitesquare.pixel :
  251.                        win->blacksquare.pixel),
  252.           GXcopy, AllPlanes);
  253.     tmpPM = XCreateBitmapFromData(win->display, win->boardwin,
  254.                 bits, SQUARE_WIDTH, SQUARE_HEIGHT);
  255.     XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),
  256.            0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,
  257.            x * (SQUARE_WIDTH + BORDER_WIDTH),
  258.            y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);
  259.     XFreePixmap(win->display, tmpPM);
  260.     }
  261.  
  262.     if (!record_english) {
  263.     gc.foreground = win->textcolor.pixel;
  264.     if (iswhite(win, x, y) || win->bnw)
  265.         gc.background = win->whitesquare.pixel;
  266.     else
  267.         gc.background = win->blacksquare.pixel;
  268.  
  269.     gc.font = win->small->fid;
  270.         
  271.     XChangeGC(win->display, DefaultGC(win->display, 0),
  272.           GCForeground | GCBackground | GCFont, &gc);
  273.         
  274.     if (!x) {
  275.         sprintf(buf, " %d", SIZE - y);
  276.         XDrawImageString(win->display, win->boardwin,
  277.                  DefaultGC(win->display, 0),
  278.                  1, (y + 1) * (SQUARE_HEIGHT + 
  279.                        BORDER_WIDTH) - BORDER_WIDTH + 
  280.                  win->small->max_bounds.ascent - 1, buf, 2);
  281.     }
  282.     if (y == SIZE - 1) {
  283.         sprintf(buf, "%c", 'A' + x);
  284.         XDrawImageString(win->display, win->boardwin,
  285.                  DefaultGC(win->display, 0),
  286.                  x * (SQUARE_WIDTH + BORDER_WIDTH) + 1,
  287.                  SIZE * (SQUARE_HEIGHT + BORDER_WIDTH) - BORDER_WIDTH + 
  288.                  win->small->max_bounds.ascent - 1, buf, 1);
  289.     }
  290.     }
  291.     return;
  292. }
  293.  
  294. void
  295. win_erasepiece(y, x, wnum)
  296.     int y, x;
  297.     color wnum;
  298. {
  299.     windata *win;
  300.     char buf[BSIZE];
  301.     XGCValues gc;
  302.     Pixmap tmpPM;
  303.     
  304.     if (oneboard || (wnum == win1->color))
  305.     win = win1;
  306.     else
  307.     win = win2;
  308.         
  309.     if (win->flipped) {
  310.     y = SIZE - y - 1;
  311.     x = SIZE - x - 1;
  312.     }
  313.  
  314.     /*
  315.       if (debug)
  316.       fprintf(stderr, "erase square (%d, %d) on board %d\n", y, x,
  317.       wnum);
  318.       */
  319.  
  320.     if ((x < 0) || (x > 7) || (y < 0) || (y > 7)) exit(1);
  321.  
  322.     if (win->bnw && !iswhite(win, x, y)) {
  323.     XSetState(win->display, DefaultGC(win->display, 0),
  324.           BlackPixel(win->display, 0),
  325.           WhitePixel(win->display, 0), GXcopy, AllPlanes);
  326.     tmpPM = XCreateBitmapFromData(win->display, win->boardwin,
  327.                 shade_bits, SQUARE_WIDTH, SQUARE_HEIGHT);
  328.  
  329.     XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),
  330.            0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,
  331.            x * (SQUARE_WIDTH + BORDER_WIDTH),
  332.            y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);
  333.  
  334.     XFreePixmap(win->display, tmpPM);
  335.     } else {
  336.     XSetFillStyle(win->display, DefaultGC(win->display, 0),
  337.               FillSolid);
  338.     XSetForeground(win->display, DefaultGC(win->display, 0),
  339.                iswhite(win, x, y) ? win->whitesquare.pixel :
  340.                win->blacksquare.pixel);
  341.     XFillRectangle(win->display, win->boardwin,
  342.                DefaultGC(win->display, 0),
  343.                x * (SQUARE_WIDTH + BORDER_WIDTH),
  344.                y * (SQUARE_HEIGHT + BORDER_WIDTH),
  345.                SQUARE_WIDTH, SQUARE_HEIGHT);
  346.     }
  347.  
  348.     if (!record_english) {
  349.     gc.foreground = win->textcolor.pixel;
  350.     if (iswhite(win, x, y) || win->bnw)
  351.         gc.background = win->whitesquare.pixel;
  352.     else
  353.         gc.background = win->blacksquare.pixel;
  354.  
  355.     gc.font = win->small->fid;
  356.         
  357.     XChangeGC(win->display, DefaultGC(win->display, 0),
  358.           GCForeground | GCBackground | GCFont, &gc);
  359.         
  360.     if (!x) {
  361.         sprintf(buf, " %d", SIZE - y);
  362.         XDrawImageString(win->display, win->boardwin,
  363.                  DefaultGC(win->display, 0),
  364.                  1, (y + 1) * (SQUARE_HEIGHT + 
  365.                        BORDER_WIDTH) - BORDER_WIDTH + 
  366.                  win->small->max_bounds.ascent - 1, buf, 2);
  367.     }
  368.     if (y == SIZE - 1) {
  369.         sprintf(buf, "%c", 'A' + x);
  370.         XDrawImageString(win->display, win->boardwin,
  371.                  DefaultGC(win->display, 0),
  372.                  x * (SQUARE_WIDTH + BORDER_WIDTH) + 1,
  373.                  SIZE * (SQUARE_HEIGHT + BORDER_WIDTH) - BORDER_WIDTH + 
  374.                  win->small->max_bounds.ascent - 1, buf, 1);
  375.     }
  376.     }
  377.     
  378.  
  379.     return;
  380. }
  381.  
  382. void
  383. win_flash(m, wnum)
  384.     move *m;
  385.     color wnum;
  386. {
  387.     windata *win;
  388.     int sx, sy, ex, ey, i;
  389.  
  390.     if (oneboard || (wnum == win1->color))
  391.         win = win1;
  392.     else
  393.         win = win2;
  394.         
  395.     if (win->flipped) {
  396.         sx = SIZE - m->fromx - 1;
  397.         sy = SIZE - m->fromy - 1;
  398.         ex = SIZE - m->tox - 1;
  399.         ey = SIZE - m->toy - 1;
  400.     } else {
  401.         sx = m->fromx;
  402.         sy = m->fromy;
  403.         ex = m->tox;
  404.         ey = m->toy;
  405.     }
  406.     sx = sx * (SQUARE_WIDTH + BORDER_WIDTH) + SQUARE_WIDTH / 2;
  407.     sy = sy * (SQUARE_HEIGHT + BORDER_WIDTH) + SQUARE_HEIGHT / 2;
  408.     ex = ex * (SQUARE_WIDTH + BORDER_WIDTH) + SQUARE_WIDTH / 2;
  409.     ey = ey * (SQUARE_HEIGHT + BORDER_WIDTH) + SQUARE_HEIGHT / 2;
  410.  
  411.     for (i = 0; i < num_flashes * 2; i++)
  412.         XDrawLine(win->display,win->boardwin,
  413.               DefaultGC(win->display, 0),
  414.               sx, sy, ex, ey);
  415.     return;
  416. }
  417.  
  418. /* Handle input from the players. */
  419.  
  420. void
  421. win_process(quick)
  422.     bool quick;
  423. {
  424.     int i, rfd = 0, wfd = 0, xfd = 0;
  425.     struct timeval timeout;
  426.  
  427.     timeout.tv_sec = 0;
  428.     timeout.tv_usec = (quick ? 0 : 500000);
  429.  
  430.     if (XPending(win1->display))
  431.         service(win1);
  432.     if (!oneboard) {
  433.         if (XPending(win1->display))
  434.         service(win2);
  435.     }
  436.  
  437.     if (oneboard)
  438.         rfd = 1 << win1->display->fd;
  439.     else
  440.         rfd = (1 << win1->display->fd) | (1 << win2->display->fd);
  441.     if (!(i = select(32, &rfd, &wfd, &xfd, &timeout)))
  442.         return;
  443.     if (i == -1) {
  444.         perror("select");
  445.         exit(1);
  446.     }
  447.     if (rfd & (1 << win1->display->fd))
  448.         service(win1);
  449.     if (!oneboard && (rfd & (1 << win2->display->fd)))
  450.         service(win2);
  451.  
  452.     return;
  453. }
  454.  
  455. static void
  456. service(win)
  457.     windata *win;
  458. {
  459.     XEvent ev;
  460.  
  461.     while(XPending(win->display)) {
  462.         XNextEvent(win->display, &ev);
  463.         if (TxtFilter(win->display, &ev))
  464.             continue;
  465.  
  466.         if (ev.xany.window == win->boardwin) {
  467.             switch (ev.type) {
  468.                 case ButtonPress:
  469.                 button_pressed(&ev, win);
  470.                 break;
  471.  
  472.                 case ButtonRelease:
  473.                 button_released(&ev, win);
  474.                 break;
  475.  
  476.                 case Expose:
  477.                 /* Redraw... */
  478.                 win_redraw(win, &ev);
  479.                 break;
  480.  
  481.                 case 0:
  482.                 case NoExpose:
  483.                 break;
  484.                 default:
  485.                 fprintf(stderr, "Bad event type %d\n", ev.type);
  486.                 exit(1);
  487.             }
  488.         } else if (ev.xany.window == win->wclockwin) {
  489.             switch (ev.type) {
  490.                 case Expose:
  491.                 clock_draw(win, WHITE);
  492.                 break;
  493.  
  494.                 case 0:
  495.                 case NoExpose:
  496.                 break;
  497.                 default:
  498.                 fprintf(stderr, "Bad event type %d\n", ev.type);
  499.                 exit(1);
  500.             }
  501.         } else if (ev.xany.window == win->bclockwin) {
  502.             switch (ev.type) {
  503.                 case Expose:
  504.                 clock_draw(win, BLACK);
  505.                 break;
  506.  
  507.                 case 0:
  508.                 case NoExpose:
  509.                 break;
  510.                 default:
  511.                 fprintf(stderr, "Bad event type %d\n", ev.type);
  512.                 exit(1);
  513.             }
  514.         } else if (ev.xany.window == win->jailwin) {
  515.             switch (ev.type) {
  516.                 case Expose:
  517.                 jail_draw(win);
  518.                 break;
  519.  
  520.                 case 0:
  521.                 case NoExpose:
  522.                 break;
  523.                 default:
  524.                 fprintf(stderr, "Bad event type %d\n", ev.type);
  525.                 exit(1);
  526.             }
  527.         } else if (ev.xany.window == win->buttonwin) {
  528.             switch (ev.type) {
  529.                 case ButtonPress:
  530.                 button_service(win, &ev);
  531.                 break;
  532.  
  533.                 case Expose:
  534.                 button_draw(win);
  535.                 break;
  536.  
  537.                 case 0:
  538.                 case NoExpose:
  539.                 break;
  540.                 default:
  541.                 fprintf(stderr, "Bad event type %d\n", ev.type);
  542.                 exit(1);
  543.             }
  544.         } else if (ev.xany.window == win->icon) {
  545.             icon_refresh(win);
  546.         } else if (ev.xany.window == win->basewin) {
  547.             message_send(win, &ev);
  548.         } else {
  549.             fprintf(stderr, "Internal Error: service: bad win\n");
  550.             fprintf(stderr, "window = %d, event = %d\n", ev.xany.window,
  551.                     ev.type);
  552.         }
  553.     }
  554.     return;
  555. }
  556.  
  557. void
  558. win_redraw(win, event)
  559.     windata *win;
  560.     XEvent *event;
  561. {
  562.     XExposeEvent *ev = &event->xexpose;
  563.     int x1, y1, x2, y2, i, j;
  564.  
  565.     drawgrid(win);
  566.     if (ev) {
  567.         x1 = ev->x / (SQUARE_WIDTH + BORDER_WIDTH);
  568.         y1 = ev->y / (SQUARE_HEIGHT + BORDER_WIDTH);
  569.         x2 = (ev->x + ev->width) / (SQUARE_WIDTH + BORDER_WIDTH);
  570.         y2 = (ev->y + ev->height) / (SQUARE_HEIGHT + BORDER_WIDTH);
  571.     } else {
  572.         x1 = 0;
  573.         y1 = 0;
  574.         x2 = SIZE - 1;
  575.         y2 = SIZE - 1;
  576.     }
  577.  
  578.     if (x1 < 0) x1 = 0;
  579.     if (y1 < 0) y1 = 0;
  580.     if (x2 < 0) x2 = 0;
  581.     if (y2 < 0) y2 = 0;
  582.     if (x1 > SIZE - 1) x1 = SIZE - 1;
  583.     if (y1 > SIZE - 1) y1 = SIZE - 1;
  584.     if (x2 > SIZE - 1) x2 = SIZE - 1;
  585.     if (y2 > SIZE - 1) y2 = SIZE - 1;
  586.  
  587.     if (win->flipped) {
  588.         y1 = SIZE - y2 - 1;
  589.         y2 = SIZE - y1 - 1;
  590.         x1 = SIZE - x2 - 1;
  591.         x2 = SIZE - x1 - 1;
  592.     }
  593.  
  594.     for (i = x1; i <= x2; i++) 
  595.         for (j = y1; j <= y2; j++) {
  596.             if (chessboard->square[j][i].color == NONE)
  597.                 win_erasepiece(j, i, WHITE);
  598.             else
  599.                 win_drawpiece(&chessboard->square[j][i], j, i,
  600.                         WHITE);
  601.             if (!oneboard) {
  602.                 if (chessboard->square[j][i].color == NONE)
  603.                     win_erasepiece(j, i, BLACK);
  604.                 else
  605.                     win_drawpiece(&chessboard->square[j][i],
  606.                             j, i, BLACK);
  607.             }
  608.         }
  609.     
  610.     return;
  611. }
  612.  
  613. static bool
  614. setup(dispname, win)
  615.     char *dispname;
  616.     windata *win;
  617. {
  618.     char buf[BSIZE], *s;
  619.     Pixmap bm, bmask;
  620.     Cursor cur;
  621.     extern char *program, *recfile;
  622.     
  623.  
  624.     if (!(win->display = XOpenDisplay(dispname)))
  625.         return (false);
  626.     
  627.  
  628.     /* Now get boolean defaults... */
  629.     if ((s = XGetDefault(win->display, program, "noisy")) && eq(s, "on"))
  630.         noisyflag = true;
  631.     if ((s = XGetDefault(win->display, program, "savemoves")) && eq(s, "on"))
  632.         saveflag = true;
  633.     if ((s = XGetDefault(win->display, program, "algebraic")) && eq(s, "on"))
  634.         record_english = false;
  635.     if ((s = XGetDefault(win->display, program, "blackandwhite")) && eq(s, "on"))
  636.         bnwflag = true;
  637.     if ((s = XGetDefault(win->display, program, "quickrestore")) && eq(s, "on"))
  638.         quickflag = true;
  639.     if ((s = XGetDefault(win->display, program, "flash")) && eq(s, "on"))
  640.         win_flashmove = true;
  641.     
  642.     /* ... numeric variables ... */
  643.     if (s = XGetDefault(win->display, program, "numflashes"))
  644.         num_flashes = atoi(s);
  645.     if (s = XGetDefault(win->display, program, "flashsize"))
  646.         flash_size = atoi(s);
  647.     
  648.     /* ... and strings. */
  649.     if (s = XGetDefault(win->display, program, "progname"))
  650.         progname = s;
  651.     if (s = XGetDefault(win->display, program, "proghost"))
  652.         proghost = s;
  653.     if (s = XGetDefault(win->display, program, "recordfile"))
  654.         recfile = s;
  655.     if (s = XGetDefault(win->display, program, "blackpiece"))
  656.         black_piece_color = s;
  657.     if (s = XGetDefault(win->display, program, "whitepiece"))
  658.         white_piece_color = s;
  659.     if (s = XGetDefault(win->display, program, "blacksquare"))
  660.         black_square_color = s;
  661.     if (s = XGetDefault(win->display, program, "whitesquare"))
  662.         white_square_color = s;
  663.     if (s = XGetDefault(win->display, program, "bordercolor"))
  664.         border_color = s;
  665.     if (s = XGetDefault(win->display, program, "textcolor"))
  666.         text_color = s;
  667.     if (s = XGetDefault(win->display, program, "textback"))
  668.         text_back = s;
  669.     if (s = XGetDefault(win->display, program, "errortext"))
  670.         error_text = s;
  671.     if (s = XGetDefault(win->display, program, "playertext"))
  672.         player_text = s;
  673.     if (s = XGetDefault(win->display, program, "cursorcolor"))
  674.         cursor_color = s;
  675.  
  676.     if ((DisplayPlanes(win->display, 0) == 1) || bnwflag)
  677.         win->bnw = true;
  678.     
  679.     /* Allocate colors... */
  680.     if (win->bnw) {
  681.         win->blackpiece.pixel = BlackPixel (win->display, 0);
  682.         win->whitepiece.pixel = WhitePixel (win->display, 0);
  683.         win->blacksquare.pixel = BlackPixel (win->display, 0);
  684.         win->whitesquare.pixel = WhitePixel (win->display, 0);
  685.         win->border.pixel = BlackPixel (win->display, 0);
  686.         win->textcolor.pixel = BlackPixel (win->display, 0);
  687.         win->textback.pixel = WhitePixel (win->display, 0);
  688.         win->playertext.pixel = BlackPixel (win->display, 0);
  689.         win->errortext.pixel = BlackPixel (win->display, 0);
  690.         win->cursorcolor.pixel = BlackPixel (win->display, 0) ;
  691.     } else {
  692.         if (!XParseColor(win->display,
  693.                  DefaultColormap(win->display, 0),
  694.                  black_piece_color, &win->blackpiece) ||  
  695.         !XParseColor(win->display,
  696.                  DefaultColormap(win->display, 0),
  697.                  white_piece_color, &win->whitepiece) ||  
  698.         !XParseColor(win->display,
  699.                  DefaultColormap(win->display, 0),
  700.                  black_square_color, &win->blacksquare) ||  
  701.         !XParseColor(win->display,
  702.                  DefaultColormap(win->display, 0),
  703.                  white_square_color, &win->whitesquare) ||  
  704.         !XParseColor(win->display,
  705.                  DefaultColormap(win->display, 0),
  706.                  border_color, &win->border) ||  
  707.         !XParseColor(win->display,
  708.                  DefaultColormap(win->display, 0),
  709.                  text_color, &win->textcolor) ||  
  710.         !XParseColor(win->display,
  711.                  DefaultColormap(win->display, 0),
  712.                  text_back, &win->textback) ||  
  713.         !XParseColor(win->display,
  714.                  DefaultColormap(win->display, 0),
  715.                  error_text, &win->errortext) ||  
  716.         !XParseColor(win->display,
  717.                  DefaultColormap(win->display, 0),
  718.                  player_text, &win->playertext) ||  
  719.         !XParseColor(win->display,
  720.                  DefaultColormap(win->display, 0),
  721.                  cursor_color, &win->cursorcolor) ||
  722.         !XAllocColor(win->display,
  723.                  DefaultColormap(win->display, 0),
  724.                  &win->blackpiece) ||  
  725.         !XAllocColor(win->display,
  726.                  DefaultColormap(win->display, 0),
  727.                  &win->whitepiece) ||  
  728.         !XAllocColor(win->display,
  729.                  DefaultColormap(win->display, 0),
  730.                  &win->blacksquare) ||  
  731.         !XAllocColor(win->display,
  732.                  DefaultColormap(win->display, 0),
  733.                  &win->whitesquare) ||   
  734.         !XAllocColor(win->display,
  735.                  DefaultColormap(win->display, 0),
  736.                  &win->border) ||  
  737.         !XAllocColor(win->display,
  738.                  DefaultColormap(win->display, 0),
  739.                  &win->textcolor) ||  
  740.         !XAllocColor(win->display,
  741.                  DefaultColormap(win->display, 0),
  742.                  &win->textback) ||  
  743.         !XAllocColor(win->display,
  744.                  DefaultColormap(win->display, 0),
  745.                  &win->errortext) ||  
  746.         !XAllocColor(win->display,
  747.                  DefaultColormap(win->display, 0),
  748.                  &win->playertext) ||  
  749.         !XAllocColor(win->display,
  750.                  DefaultColormap(win->display, 0),
  751.                  &win->cursorcolor))   
  752.         fprintf(stderr, "Can't get color...\n");
  753.     }
  754.  
  755.     /* Get fonts... */
  756.     win->small = XLoadQueryFont(win->display,SMALL_FONT);
  757.     win->medium = XLoadQueryFont(win->display,MEDIUM_FONT);
  758.     win->large = XLoadQueryFont(win->display,LARGE_FONT);
  759.     
  760.     /* Create the windows... */
  761.  
  762.     win->basewin =
  763.         XCreateSimpleWindow(win->display,DefaultRootWindow(win->display),
  764.               BASE_XPOS, BASE_YPOS, 
  765.               BASE_WIDTH, BASE_HEIGHT, 0,
  766.               BlackPixel(win->display, 0),
  767.               WhitePixel(win->display, 0)); 
  768.     win->boardwin = XCreateSimpleWindow(win->display,win->basewin,
  769.                         BOARD_XPOS, BOARD_YPOS, 
  770.                         BOARD_WIDTH, BOARD_HEIGHT,
  771.                         BORDER_WIDTH,
  772.                         win->border.pixel,
  773.                         WhitePixel(win->display, 0));
  774.     win->recwin = XCreateSimpleWindow(win->display,win->basewin,
  775.                       RECORD_XPOS, RECORD_YPOS,
  776.                       RECORD_WIDTH, RECORD_HEIGHT,
  777.                       BORDER_WIDTH, win->border.pixel,
  778.                       win->textback.pixel);
  779.     win->jailwin = XCreateSimpleWindow(win->display,win->basewin,
  780.                        JAIL_XPOS, JAIL_YPOS,
  781.                        JAIL_WIDTH, JAIL_HEIGHT,
  782.                        BORDER_WIDTH,
  783.                        win->border.pixel,
  784.                        win->textback.pixel);
  785.     win->wclockwin = XCreateSimpleWindow(win->display,win->basewin,
  786.                          WCLOCK_XPOS, WCLOCK_YPOS,
  787.                          CLOCK_WIDTH, CLOCK_HEIGHT,
  788.                          BORDER_WIDTH, win->border.pixel,
  789.                          win->textback.pixel);
  790.     win->bclockwin = XCreateSimpleWindow(win->display,win->basewin,
  791.                          BCLOCK_XPOS, BCLOCK_YPOS,
  792.                          CLOCK_WIDTH, CLOCK_HEIGHT,
  793.                          BORDER_WIDTH, win->border.pixel,
  794.                          win->textback.pixel);
  795.     win->messagewin = XCreateSimpleWindow(win->display,win->basewin,
  796.                           MESS_XPOS, MESS_YPOS,
  797.                           MESS_WIDTH, MESS_HEIGHT,
  798.                           BORDER_WIDTH, win->border.pixel,
  799.                           win->textback.pixel);
  800.     win->buttonwin = XCreateSimpleWindow(win->display,win->basewin,
  801.                          BUTTON_XPOS, BUTTON_YPOS,
  802.                          BUTTON_WIDTH, BUTTON_HEIGHT,
  803.                          BORDER_WIDTH, win->border.pixel,
  804.                          win->textback.pixel);
  805.     
  806.     /* Let's define an icon... */
  807.     win->iconpixmap = XCreatePixmapFromBitmapData(win->display,
  808.                               win->basewin, icon_bits,
  809.                               icon_width, icon_height,
  810.                               win->blacksquare.pixel,
  811.                               win->whitesquare.pixel,
  812.                               1);
  813.     
  814.     bm = XCreateBitmapFromData(win->display,
  815.                    win->basewin, xchess_bits,
  816.                    xchess_width, xchess_height);
  817.     bmask = XCreateBitmapFromData(win->display,
  818.                    win->basewin, xchess_mask_bits,
  819.                    xchess_width, xchess_height);
  820.     cur = XCreatePixmapCursor(win->display, bm, bmask,
  821.                 &win->cursorcolor,
  822.                 &WhitePixel(win->display, 0),
  823.                 xchess_x_hot, xchess_y_hot);
  824.     XFreePixmap(win->display, bm);
  825.     XFreePixmap(win->display, bmask);
  826.     
  827.     XDefineCursor(win->display,win->basewin, cur);
  828.  
  829.     XMapSubwindows(win->display,win->basewin);
  830.     XMapRaised(win->display,win->basewin);
  831.  
  832.     XSelectInput(win->display,win->basewin, KeyPressMask);
  833.     XSelectInput(win->display,win->boardwin,
  834.              ButtonPressMask | ButtonReleaseMask | ExposureMask);
  835.     XSelectInput(win->display,win->recwin,
  836.              ButtonReleaseMask | ExposureMask);
  837.     XSelectInput(win->display,win->jailwin, ExposureMask);
  838.     XSelectInput(win->display,win->wclockwin, ExposureMask);
  839.     XSelectInput(win->display,win->bclockwin, ExposureMask);
  840.     XSelectInput(win->display,win->messagewin,
  841.              ButtonReleaseMask | ExposureMask);
  842.     XSelectInput(win->display,win->buttonwin,
  843.              ButtonPressMask | ExposureMask);
  844.     
  845.     message_init(win);
  846.     record_init(win);
  847.     button_draw(win);
  848.     jail_init(win);
  849.     clock_init(win, WHITE);
  850.     clock_init(win, BLACK);
  851.     if (timeunit) {
  852.         if (timeunit > 1800)
  853.             sprintf(buf, "%d moves every %.2lg hours.\n",
  854.                 movesperunit, ((double) timeunit) / 3600);
  855.         else if (timeunit > 30)
  856.             sprintf(buf, "%d moves every %.2lg minutes.\n",
  857.                 movesperunit, ((double) timeunit) / 60);
  858.         else
  859.             sprintf(buf, "%d moves every %d seconds.\n",
  860.                 movesperunit, timeunit);
  861.         message_add(win, buf, false);
  862.     }
  863.     
  864.     return (true);
  865. }
  866.  
  867. static void
  868. drawgrid(win)
  869.     windata *win;
  870. {
  871.     int i;
  872.     XGCValues gc;
  873.  
  874.     gc.function = GXcopy;
  875.     gc.plane_mask = AllPlanes;
  876.     gc.foreground = win->border.pixel;
  877.     gc.line_width = 0;
  878.     gc.line_style = LineSolid;
  879.     
  880.     XChangeGC(win->display,
  881.           DefaultGC(win->display, 0),
  882.           GCFunction | GCPlaneMask | GCForeground |
  883.           GCLineWidth | GCLineStyle, &gc);
  884.     
  885.     /* Draw the lines... horizontal, */
  886.     for (i = 1; i < SIZE; i++)
  887.         XDrawLine(win->display, win->boardwin,
  888.               DefaultGC(win->display, 0), 0,
  889.               i * (SQUARE_WIDTH + BORDER_WIDTH) -
  890.                   BORDER_WIDTH / 2,
  891.               SIZE * (SQUARE_WIDTH + BORDER_WIDTH),
  892.               i * (SQUARE_WIDTH + BORDER_WIDTH) -
  893.                   BORDER_WIDTH / 2);
  894.  
  895.     /* and vertical... */
  896.     for (i = 1; i < SIZE; i++)
  897.         XDrawLine(win->display, win->boardwin,
  898.               DefaultGC(win->display, 0),
  899.               i * (SQUARE_WIDTH + BORDER_WIDTH) -
  900.                 BORDER_WIDTH / 2, 0,
  901.               i * (SQUARE_WIDTH + BORDER_WIDTH) -
  902.                     BORDER_WIDTH / 2, 
  903.               SIZE * (SQUARE_WIDTH + BORDER_WIDTH));
  904.     return;
  905. }
  906.  
  907. void
  908. win_restart()
  909. {
  910.     win1->flipped = false;
  911.     win_redraw(win1, (XEvent *) NULL);
  912.     if (!oneboard) {
  913.         win2->flipped = true;
  914.         win_redraw(win2, (XEvent *) NULL);
  915.     }
  916.     return;
  917. }
  918.  
  919. static void
  920. icon_refresh(win)
  921.     windata *win;
  922. {
  923.     XCopyArea(win->display, win->iconpixmap, win->icon,
  924.           DefaultGC(win->display, 0),
  925.           0, 0, icon_width, icon_height, 0, 0);
  926.     return;
  927. }
  928.  
  929.